//
//  HHTenViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/6.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHSevenViewController.h"
#import "HHScrollPageView.h"
#import "HHTest1ViewController.h"
#import "HHTabController.h"

@interface HHSevenViewController ()<HHScrollPageViewDelegate>
@property(nonatomic, weak) HHScrollPageView *scrollView;
@property(nonatomic, strong) NSArray<NSString *> *titles;
@property(nonatomic, strong) NSArray<UIViewController <HHScrollPageViewChildDelegate> *> *childVc;

@end
/*
 1. 导入 #import "HHScrollPageView.h"
 2.设置HHScrollPageViewDelegate
 并实现下面两个方法:
 // 将要显示的子页面的总数
-(NSInteger)numberOfChildViewControllers;

 // 获取到将要显示的页面的控制器
 reuseViewController : 这个是返回给你的controller, 你应该首先判断这个是否为nil, 如果为nil 创建对应的控制器并返回, 如果不为nil直接使用并返回
  index : 对应的下标
- (UIViewController<HHScrollPageViewChildDelegate> *)childViewController:(UIViewController<HHScrollPageViewChildDelegate> *)reuseViewController forIndex:(NSInteger)index;
 3.设置style样式
 
 */

@implementation HHSevenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    HHSegmentStyle *style = [[HHSegmentStyle alloc]init];
    //显示遮盖
    style.showLine = YES;
    
    //显示高度
    style.segmentHeight= 60;
    //显示图片  (在显示图片的时候只有下划线的效果可以开启, 其他的'遮盖','渐变',效果会被内部关闭)
    style.showImage = YES;
    
    //图片的位置
    style.imagePosition = TitleImagePositionTop;
    
    //开始滚动就改变标题
    style.adjustTitleWhenBeginDrag = YES;
    
    //// 当标题(和图片)宽度总和小于ZJScrollPageView的宽度的时候, 标题会自适应宽度
    style.autoAdjustTitlesWidth = YES;
    
    self.titles = @[@"发现",@"消息",@"订单",@"个人中心"];
    HHScrollPageView *scrollView =[[HHScrollPageView alloc]initWithFrame:CGRectMake(0, 0, screen_width, screen_height-64) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    
    self.scrollView = scrollView;
    [self.view addSubview: scrollView];
    

}
-(void)setUpTitleView:(HHTitleView *)titleView forIndex:(NSInteger)index{
    titleView.normalImage =[UIImage imageNamed:[NSString stringWithFormat:@"normal-%zd",index+1]];
    titleView.selectedImage =[UIImage imageNamed:[NSString stringWithFormat:@"selected-%zd",index+1]];

}
-(NSInteger)numberOfChildViewControllers{

    return self.titles.count;

}

- (UIViewController<HHScrollPageViewChildDelegate> *)childViewController:(UIViewController<HHScrollPageViewChildDelegate> *)reuseViewController forIndex:(NSInteger)index{
//使用注意:
    //根据index 创建不同的子控制器或者title返回相应的控制器, 但是控制器必须要遵守 HHScrollPageViewChildDelegate
    // 注意HHScrollPageView不会保证viewWillAppear等生命周期方法一定会调用
    // 所以建议使用HHScrollPageViewChildDelegate中的方法来加载不同的数据
    if (index == 0) {
        // 注意这个效果和tableView的deque...方法一样, 会返回一个可重用的childVc
        // 请首先判断返回给你的是否是可重用的
        // 如果为nil就重新创建并返回
        // 如果不为nil 直接使用返回给你的reuseViewController并进行需要的设置 然后返回即可
        HHTabController *childVC =(HHTabController *)reuseViewController;
        if (childVC ==nil) {
            childVC =[[HHTabController alloc]init];
        }
        return childVC;
        
    }else if (index ==1){
        HHTest1ViewController *childVC =(HHTest1ViewController *)reuseViewController;
        if (childVC ==nil) {
            childVC =[[HHTest1ViewController alloc]init];
            childVC.view.backgroundColor =[UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        }
        return childVC;

    
    
    }else if(index == 2){
    
        HHTest2ViewController *childVC =(HHTest2ViewController *)reuseViewController;
        if (childVC ==nil) {
            childVC =[[HHTest2ViewController alloc]init];
            childVC.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];;
        }
        return childVC;
    
    }else{
        HHTest1ViewController *childVC =(HHTest1ViewController *)reuseViewController;
        if (childVC ==nil) {
            childVC =[[HHTest1ViewController alloc]init];
            childVC.view.backgroundColor =[UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        }
        return childVC;
    
    
    }

}

@end
